package com.xbongbong.saas.help;

import com.xbongbong.paas.pojo.dto.ValidateDataDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.service.DistributorAccountService;
import com.xbongbong.saas.service.DistributorCommunicateService;
import com.xbongbong.saas.service.DistributorContactService;
import com.xbongbong.saas.service.DistributorManageService;
import com.xbongbong.saas.service.OrderOutstockService;
import com.xbongbong.saas.service.OrderService;
import com.xbongbong.saas.service.ReturnService;
import com.xbongbong.saas.service.UserTeamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 经销商专用
 *
 * @author xinpeng.jiang
 * @version 1.0
 * @date 2020/12/22 15:54
 * @since V1.0
 */
@Component
public class DistributorSaveHelp {

    private static final Logger LOG = LoggerFactory.getLogger(DistributorSaveHelp.class);

    @Resource
    private DistributorManageService distributorManageService;
    @Resource
    private UserTeamService userTeamService;
    @Resource
    private DistributorAccountService distributorAccountService;
    @Resource
    private DistributorContactService distributorContactService;
    @Resource
    private OrderService orderService;
    @Resource
    private DistributorCommunicateService distributorCommunicateService;
    @Resource
    private ReturnService returnService;
    @Resource
    private SaasSaveHelp saasSaveHelp;
    @Resource
    private OrderOutstockService orderOutstockService;

    /**
     * 字段保存前特殊校验
     *
     * @param validateDataDTO 数据对象
     * @throws XbbException 抛出业务异常
     * @author xinpeng.jiang
     */
    public void beforeSave(ValidateDataDTO validateDataDTO) throws XbbException {
        Integer businessType = validateDataDTO.getBusinessType();
        // 标签设置
        saasSaveHelp.dealLabelData(validateDataDTO.getData(), validateDataDTO.getExplainMap());
        if (Objects.equals(SaasMarkEnum.PAAS.getCode(), validateDataDTO.getSaasMark())) {
            userTeamService.beforeVerifyUserTeam(validateDataDTO);
            return;
        }
        if (businessType == null || businessType == 0) {
            LOG.error("businessType为空");
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100002);
        }
        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(businessType);
        switch (xbbRefTypeEnum) {
            case CUSTOMER_MANAGEMENT:
                distributorManageService.beforeSave(validateDataDTO);
                userTeamService.beforeVerifyUserTeam(validateDataDTO);
                break;
            case DISTRIBUTOR_ACCOUNT:
                distributorAccountService.beforeSave(validateDataDTO);
                break;
            case CONTACT:
                distributorContactService.beforeSave(validateDataDTO);
                break;
            case CUSTOMER_COMMUNICATE:
                distributorCommunicateService.beforeSave(validateDataDTO);
                break;
            case CONTRACT:
                orderService.beforeSave(validateDataDTO);
                userTeamService.beforeVerifyUserTeam(validateDataDTO);
                break;
            case REFUND:
                returnService.beforeSave(validateDataDTO);
                userTeamService.beforeVerifyUserTeam(validateDataDTO);
                break;
            case CONTRACT_OUTSTOCK:
                orderOutstockService.beforeSave(validateDataDTO);
                break;
            default:
                saasSaveHelp.beforeSave(validateDataDTO);
                break;
        }
    }

}
